<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dettaglio alunno</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../../css/main.css" rel="stylesheet" />
<link href="../../css/skins/aqua/theme.css" type="text/css" rel="stylesheet"  />
<script type="text/javascript" src="../../js/prototype.js"></script>
<script type="text/javascript" src="../../js/scriptaculous.js"></script>
<script type="text/javascript" src="../../js/controls.js"></script>
<script type="text/javascript" src="../../js/page.js"></script>
<script type="text/javascript" src="../../js/calendar.js"></script>
<script type="text/javascript" src="../../js/lang/calendar-it.js"></script>
<script type="text/javascript" src="../../js/calendar-setup.js"></script>
<script type="text/javascript">
function go(par, student){
    if(par == 2){
        if(!confirm("Sei sicuro di voler cancellare questo alunno?"))
            return false;
    }
    $('_i').value = student;
    $('action').value = par;
    var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_students/student_manager.php";
    //alert(url);
	var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: $('st_form').serialize(true),
			    	onSuccess: function(transport){
			      		var response = transport.responseText || "no response text";
			      		//alert(response);
			      		var dati = response.split("|");
			      		if(dati[0] == "ko"){
							alert("Errore SQL. \nQuery: "+dati[1]+"\nErrore: "+dati[2]);
							return;
			      		}
			      		//alert(dati[1]);
			      		//alert(dati[2]);
			      		parent.usr_win.close();
			      		link = "alunni.php?msg="+par;
			      		if(par != 1){
							link += "&second=1&offset=<?php print $offset ?>";
			      		}

						parent.document.location.href = link;
			      		//parent.win.close();
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore...");}
			  });
	  
}

function verifica(){
    //alert("ok");
	var nick = document.forms[0].uname.value;
	var url = "<?php print $_SESSION['__config__']['root_site'] ?>shared/verifica_login.php";
	var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: {nick: nick},
			    	onSuccess: function(transport){
			    		var x = document.getElementById("check");
			            x.innerHTML = "";
			      		var response = transport.responseText || "no response text";
			      		if(response == "ko"){
			      			x.style.color = "red";
			                x.innerHTML = "<br />Login gi&agrave; presente.";
			                return;
			     		}
			     		else{
			     			x.innerHTML = "<img src='/img/check.png' style='width: 15px; height: 15px; vertical-align: bottom' />";
			            	new_account = true;
			            	document.getElementById("account_button").onclick = account_wrapper;
			     		}
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore..."); }
			  });
}

function reg(par){
    var id = <?php print $_REQUEST['id'] ?>;
    if(par == 1){
		var nick = document.forms[0].uname.value;
		var pwd =  document.forms[0].pwd.value;
		var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_students/modifica_account.php";
		var req = new Ajax.Request(url,
				  {
				    	method:'post',
				    	parameters: {nick: nick, pwd: pwd, id: id},
				    	onSuccess: function(transport){
				    		var x = document.getElementById("check");
				            x.innerHTML = "";
				      		var response = transport.responseText || "no response text";
				      		if(response == "ko"){
				      			alert(response);
				                return;
				     		}
				     		else{
				     			new_account = false;
								document.getElementById("account_button").onclick = no_change;
								var field = document.getElementById("account_field");
								var legend = document.getElementById("account_legend");
								field.style.border = "1px solid ";
								field.style.color = "#000000";
								alert("Account modificato correttamente");
				     		}
				    	},
				    	onFailure: function(){ alert("Si e' verificato un errore..."); }
				  });
	}
}

function gen_pwd(){
	var pass = genera_password(document.forms[0].nome.value, document.forms[0].cognome.value);
	document.forms[0].pwd.value = pass;
	new_account = true;
	document.getElementById("account_button").onclick = account_wrapper;
	//alert(document.getElementById("account_button").onclick);
	var field = document.getElementById("account_field");
	var legend = document.getElementById("account_legend");
	field.style.border = "1px solid #ff0000";
	field.style.color = "#ff0000";
}

function gen_login(){
	if((trim(document.forms[0].nome.value) == "") || (trim(document.forms[0].cognome.value) == "")){
		alert("Inserisci nome e cognome per generare la username");
		return;
	}
	var nome = document.forms[0].nome.value;
	var cognome = document.forms[0].cognome.value;
	var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_students/crea_login.php";
	var data = "nome="+nome+"&cognome="+cognome;
	var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: {nome: nome, cognome: cognome},
			    	onSuccess: function(transport){
			    		var x = document.getElementById("check");
			            x.innerHTML = "";
			      		var response = transport.responseText || "no response text";
			      		if(response == "ko"){
			      			alert(response);
			                return;
			     		}
			     		else{
			     			document.forms[0].us.value = response;
			     		}
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore...") }
			  });
}

function no_change(){
	alert("Nessun dato modificato");
}

function account_wrapper(){
	reg(1);
}

</script>
</head>
<body <?php if(isset($msg)) print("onLoad=\"alert('".$msg."'); window.close(); window.opener.document.location.href='alunni.php?second=1&offset=".$offset."&order=".$_REQUEST['order']."'\"") ?>>
    <p style="font-size: 12px; font-weight: bold; text-align: center; padding-top: 15px">Gestione utente</p>
    <form action="dettaglio_alunno.php?upd=1&offset=<?php print $offset ?>&order=<?php print $_REQUEST['order'] ?>" method="post" id="st_form">
    <fieldset id="account_field" style="width: 550px; border: 1px solid; padding-top: 10px; margin-left: auto; margin-right: auto">
    <legend id="account_legend" style="font-weight: bold;">Account</legend>
    <table style="width: 530px">
        <tr class="popup_row header_row">
            <td class="popup_title">UserName</td>
            <td style="width: 250px">
                <input class="form_input" id="us" type="text" name="uname" style="width: 200px" value="<?php if(isset($alunno)) print($alunno['username']) ?>" <?php if($_i == 0) print("readonly='readonly'")  ?> />
                <span id="check"></span>
            </td>
            <td style="width: 130px">
                <!-- <input type="button" onclick="verifica()" value="Verifica username" style="border: 1px solid; float: right; display: inline" />  -->
                <?php if($_i != 0){ ?>
                <a href="#" onclick="verifica()">Verifica username</a>
                <?php 
				}else{
                ?>
                <a href="#" onclick="gen_login()">Crea username</a>
                <?php } ?>
                <span id="verify"></span>
            </td>
        </tr>
        <tr class="popup_row">
            <td class="popup_title" style="width: 150px">Password</td>
            <td style="width: 250px">
                <input type="password" name="pwd" style="width: 200px" class="form_input" value="" readonly="readonly" />
            </td>
            <td style="width: 130px">
              <!--   <input type="button" onclick="gen_pwd('<?php print $alunno['nome'] ?>', '<?php print $alunno['cognome'] ?>')" value="Genera  Password" style="border: 1px solid; float: right; display: inline" /> -->
              <a href="#" onclick="gen_pwd()">Genera password</a>
            </td>
        </tr>
        <tr class="popup_row">
            <td colspan="2" style="height: 5px"></td>
            <td>
            	<!-- <input type="button" id="account_button" onclick="reg(1)" value="Registra   account" style="border: 1px solid; float: right; display: inline" disabled="disabled" /> -->
            	<?php if($_i != 0){ ?>
            	<a href="#" id="account_button" onclick="alert('Nessun dato modificato')">Registra account</a>
            	<?php } ?>
            </td>
        </tr>
    </table>
    </fieldset>
    <fieldset style="width: 550px; border: 1px solid; padding-top: 10px; margin-top: 30px; margin-left: auto; margin-right: auto">
    <legend style="font-weight: bold">Dati personali</legend>
    <table width="530">
        <tr class="popup_row header_row">
            <td class="popup_title" style="width: 150px">Nome</td>
            <td colspan="2" style="width: 380px">
                <input class="form_input" type="text" name="nome" style="width: 380px" value="<?php if(isset($alunno)) print($alunno['nome']) ?>" />
            </td>
        </tr>
        <tr class="popup_row">
            <td class="popup_title" style="width: 150px">Cognome</td>
            <td colspan="2" style="width: 380px">
                <input class="form_input" type="text" name="cognome" style="width: 380px" value="<?php if(isset($alunno)) print($alunno['cognome']) ?>" />
            </td>
        </tr>
        <tr class="popup_row">
            <td class="popup_title" style="width: 150px">Codice fiscale</td>
            <td colspan="2" style="width: 380px">
                <input class="form_input" type="text" name="cf" style="width: 180px" value="<?php if(isset($alunno)) print($alunno['codice_fiscale']) ?>" style="width: 380px; font-size: 11px" />
            	<span class="popup_title" style="padding-left: 16px">Sesso</span>
            	<select class="form_input" name="sesso" style="width: 145px">
            		<option value="M" <?php if(isset($alunno) && ($alunno['sesso'] == "M")) print("selected='selected'") ?>>Maschio</option>
            		<option value="F" <?php if(isset($alunno) && ($alunno['sesso'] == "F")) print("selected='selected'") ?>>Femmina</option>
            	</select>
            </td>
        </tr>
        <tr class="popup_row">
            <td class="popup_title" style="width: 150px">Data di nascita</td>
            <td colspan="2" style="width: 380px">
                <input class="form_input" type="text" id="sel3" name="data_nascita" style="width: 180px" value="<?php if(isset($alunno)) print(format_date($alunno['data_nascita'], SQL_DATE_STYLE, IT_DATE_STYLE, "/")) ?>" />
                <script type="text/javascript">
                <?php 
                if(isset($alunno)){
                	list($y, $m, $d) = split("-", $alunno['data_nascita']);
                	$m--;
                }
                ?>
	            Calendar.setup({
	                date		: new Date(<?php if(isset($alunno)) print("$y, $m, $d") ?>),
					inputField	: "sel3",
					ifFormat	: "%d/%m/%Y",
					showsTime	: false,
					firstDay	: 1,
					timeFormat	: "24"					
				});
	        	</script>
                <span class="popup_title" style="padding-left: 10px">Classe</span>
            	<select class="form_input" name="classe" style="width: 145px; margin-left: 1px">
            		<option value="all">Scegli una classe</option>
            	<?php
				while($cls = $res_classi->fetch_assoc()){
            	?>
            		<option <?php if($cls['id_classe'] == $alunno['id_classe']) print("selected='selected'") ?> value="<?php print $cls['id_classe'].";".$cls['anno_corso'].$cls['sezione'] ?>"><?php print $cls['anno_corso'].$cls['sezione'] ?></option>
            	<?php
				}
            	?>
            	</select>
            </td>
        </tr>
        <tr class="popup_row">
            <td colspan="3" style="height: 1px">
            	<input type="hidden" name="action" id="action" />
    			<input type="hidden" name="_i" id="_i" />
            </td>
        </tr>       
    </table>
    </fieldset> 
    <div style="width: 650px;  margin-top: 50px; padding-bottom: 30px; text-align: right">
        <a href="#" onclick="go(<?php if(isset($_REQUEST['id']) && $_REQUEST['id'] != 0) print("3, ".$_REQUEST['id']); else print("1, 0"); ?>)" class="nav_link_first">Registra</a>|
        <?php if(isset($_REQUEST['id']) && $_REQUEST['id'] != 0){
        ?>
        <a href="#" onclick="go(2, <?php print $_REQUEST['id'] ?>)" class="nav_link">Cancella utente</a>|
        <?php
        }
        ?>
        <a href="#" onclick="parent.usr_win.close()" style="margin-right: 52px;" class="nav_link_last">Chiudi</a>
    </div>
   </form>
</body>
</html>